﻿using B.S.DataBase.MyProject.Domain.Procurements;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.ProcurementsCommand;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.ProcurementsCommandCommand
{
    /// <summary>
    /// 采购到货审核
    /// </summary>
    public class AddUpdateAlivCommdHandler : IRequestHandler<AddUpdateAlivCommd, APIResult<object>>
    {
        private readonly IBaseRepository<PurchaseArrival> baseRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repository"></param>
        public AddUpdateAlivCommdHandler(IBaseRepository<PurchaseArrival> repository)
        {
            this.baseRepository = repository;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<APIResult<object>> Handle(AddUpdateAlivCommd request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            result.Code = APIEnum.修改成功;
            result.Msg = APIEnum.修改成功.ToString();
            var list = baseRepository.GetAll().Result.Where(X => X.PurchaseArrivaId == request.PurchaseArrivaId).FirstOrDefault();
            if (list.BocumentStatus == "开立")
            {
                list.BocumentStatus = "审核";
                list.PurchasingDe = "ZC";
            }
            else if(list.BocumentStatus=="完成")
            {
                list.BocumentStatus = "审核";
                list.PurchasingDe = "ZC";
            }
            else
            {
                list.BocumentStatus = "完成";
                list.PurchasingDe = "admin";
            }
            baseRepository.Update(list);
            return Task.FromResult(result);
        }
    }
}
